import { getWxParam, getAuthUrl, getUserInfo } from '@/api/user'
import { removeLocalStorage } from '@/utils'
const user = {
  state: {
    userInfo: null // 用户信息数据
  },
  mutations: {
    SET_USERINFO_DATA(state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    // 获取用户基本信息
    GetUserInfo({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        getUserInfo(parameterData)
          .then(response => {
            commit('SET_USERINFO_DATA', response.data.userInfo)
            resolve()
          })
          .catch(() => {
            removeLocalStorage('share_token')
          })
      })
    },
    // 登录
    GetWxParam({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        getWxParam(parameterData)
          .then(response => {
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    GetAuthUrl({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        getAuthUrl(parameterData)
          .then(response => {
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 前端 登出 清除本地token 不访问接口
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        removeLocalStorage('share_token')
        commit('SET_USERINFO_DATA', null)
        resolve()
      })
    }
  }
}

export default user
